<template>
	<view class="play-order-content">
		<view class="play-order-view">
			<text>区域：</text>
			<text>{{ position_name }}</text>
		</view>
		<view style="font-size: 32upx;">
			<text>商品名称：</text>
			<text>{{ coommodityname }}</text>
		</view>
		<view class="play-order-view">
			<text>规格：</text>
			<text>{{ detailInfo.product_variant }}</text>
		</view>
		<!-- page_type 类型为0 时 -->
		<view v-if="zerotate">
			<view class="required-number play-order-view">
				<text>所需数量：</text>
				<input v-model="detailInfo.need_qty" @input="getUsername" placeholder="数量" />
				<text>件</text>
			</view>
			<view class="required-number play-order-view">
				<text>采购数量：</text>
				<text>{{ detailInfo.product_qty }}</text>
				<!-- <input v-model="detailInfo.product_qty" placeholder="数量" disabled="true" /> -->
				<text>件</text>
			</view>
		</view>
		<!-- page_type 类型为1 时 -->
		<view v-if="firststate">
			<view class="required-number play-order-view">
				<text>面积：</text>
				<input v-model="detailInfo.position_area" @input="getfirst" placeholder="面积" />
				<text>m²</text>
			</view>
			<view class="required-number play-order-view">
				<text class="cus-title-nowrap">损耗系数：</text>
				<input v-model="detailInfo.loss_coefficient" placeholder="系数" @input="lossat" />
				<text>%</text>
			</view>
			<view class="required-number play-order-view">
				<text class="cus-title-nowrap">所需数量：</text>
				<input v-model="detailInfo.need_qty" @input="getfirstat" placeholder="数量" />
				<text>片</text>
			</view>
			<view class="required-number play-order-view">
				<text class="cus-title-nowrap">采购数量：</text>
				<input v-model="detailInfo.product_qty" placeholder="数量" disabled="true" />
				<text>片</text>
			</view>
		</view>
		<!-- 类型 类型为2 时 -->
		<view v-if="secondstate">
			<view class="required-number play-order-view">
				<text class="cus-title-nowrap">面积：</text>
				<input v-model="detailInfo.position_area" placeholder="面积" @input="getsecond" />
				<text>m²</text>
			</view>
			<view class="required-number play-order-view">
				<text class="cus-title-nowrap">损耗系数：</text>
				<input v-model="detailInfo.loss_coefficient" placeholder="系数" @input="loss" />
				<text>%</text>
			</view>
			<view class="required-number play-order-view">
				<text class="cus-title-nowrap">所需数量：</text>
				<input v-model="detailInfo.need_qty" @input="getsecondat" placeholder="数量" />
				<text>片</text>
			</view>
			<view class="required-number play-order-view">
				<text class="cus-title-nowrap">采购数量：</text>
				<input v-model="detailInfo.product_qty" placeholder="数量" disabled="true" />
				<text>箱</text>
			</view>
		</view>

		<button style="margin-top: 40upx;" type="primary" @click="rightgood">确定</button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			zerotate: false, // age_type 类型为0
			firststate: false, // age_type 类型为1
			secondstate: false, // age_type 类型为2
			detailInfo: {},
			msg: '下单',
			wo_material_id: '', // 材料明细行ID
			product_id: '', //商品ID
			coommodityname: '', // 商品名称
			position_name: '', // 区域
			userName: '',
			ruleVariable1: '', //单位变量为BOX
			ruleVariable2: '', //单位变量为slice
			uom_1_id: '',
			contsnumber: ''
		};
	},
	onLoad(param) {
		this.wo_material_id = param.wo_material_id;
		this.product_id = param.product_id;
		this.coommodityname = param.name;
		this.position_name = param.position_name;
		this.getgood(parseInt(param.wo_material_id));
	},
	methods: {
		// 监听Input
		getUsername(e) {
			this.detailInfo.product_qty = e.detail.value;
			console.log(this.detailInfo.product_qty);
		},
		// 类型为1 计算
		getfirst(e) {
			// detailInfo.loss_coefficient
			this.detailInfo.position_area = Math.ceil((this.detailInfo.loss_coefficient / 100 + 1) * e.detail.value);
			this.detailInfo.need_qty = Math.ceil(e.detail.value / this.ruleVariable2);
			this.contsnumber = e.detail.value;
			this.detailInfo.product_qty = this.detailInfo.need_qty;
		},
		getfirstat(e) {
			this.detailInfo.product_qty = e.detail.value;
		},
		lossat(e) {
			console.log((e.detail.value / 100 + 1) * this.contsnumber);
			this.detailInfo.position_area = Math.ceil((e.detail.value / 100 + 1) * this.contsnumber);
			this.detailInfo.need_qty = Math.ceil(this.detailInfo.position_area / this.ruleVariable2);
			this.detailInfo.product_qty = this.detailInfo.need_qty;
		},
		// 类型为2 计算

		getsecond(e) {
			this.detailInfo.position_area = Math.ceil((this.detailInfo.loss_coefficient / 100 + 1) * e.detail.value);
			this.detailInfo.need_qty = Math.ceil(e.detail.value / this.ruleVariable2);
			this.contsnumber = e.detail.value;
			this.detailInfo.product_qty = Math.ceil(this.detailInfo.need_qty / this.ruleVariable1);
		},
		getsecondat(e) {
			this.detailInfo.product_qty = Math.ceil(e.detail.value / this.ruleVariable1);
		},
		loss(e) {
			console.log((e.detail.value / 100 + 1) * this.contsnumber);
			this.detailInfo.position_area = Math.ceil((e.detail.value / 100 + 1) * this.contsnumber);
			this.detailInfo.need_qty = Math.ceil(this.detailInfo.position_area / this.ruleVariable2);
			this.detailInfo.product_qty = Math.ceil(this.detailInfo.need_qty / this.ruleVariable1);
		},
		rightgood() {
			if (this.detailInfo.position_area <= 0) {
				uni.showToast({
					title: '面积要大于0',
					mask: false,
					duration: 1500,
					icon: 'none'
				});
			}
			if (this.detailInfo.position_area > 0) {
				uni.request({
					url: this.$store.state.baseUrl + '/lwj/get_material_order_write',
					method: 'POST',
					header: {
						token: this.$store.state.token
					},
					data: {
						wo_material_id: parseInt(this.wo_material_id),
						product_id: parseInt(this.product_id),
						position_area: parseFloat(this.detailInfo.position_area),
						need_qty: parseFloat(this.detailInfo.need_qty),
						product_qty: parseFloat(this.detailInfo.product_qty),
						uom_1_id: this.uom_1_id
					},
					success: res => {
						if (res.data.code === 1) {
							uni.showToast({
								title: '材料下单成功',
								mask: false,
								duration: 1500,
								icon: 'none',
								success: res => {
									setTimeout(() => {
										console.log('123');
										this.ReturnSuperiors();
									}, 1500);
								}
							});
						} else {
							uni.showToast({
								title: res.data.msg,
								mask: false,
								duration: 1500,
								icon: 'none'
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});
			}
		},
		uomlines(line) {
			for (let i = 0; i < line.length; i++) {
				if (line[i].uom_type == 'box') {
					this.ruleVariable1 = line[i].quantity_2;
					this.uom_1_id = line[i].uom_1_id;
				} else {
					this.uom_1_id = line[0].uom_1_id;
				}
				if (line[i].uom_type == 'slice') {
					this.ruleVariable2 = line[i].quantity_2;
				}
			}
		},
		// 下单详情
		getgood(id) {
			this.$request({
				url: this.$store.state.baseUrl + '/lwj/get_material_order_info',
				method: 'POST',
				header: {
					token: this.$store.state.token
				},
				data: {
					wo_material_id: id
				},
				success: res => {
					if (res.data.code === 1) {
						this.detailInfo = res.data.data.info;
						this.contsnumber = this.detailInfo.position_area;
						// 下单类型判断
						if (this.detailInfo.page_type == 0) {
							this.zerotate = true;
						}
						if (this.detailInfo.page_type == 1) {
							this.firststate = true;
						}
						if (this.detailInfo.page_type == 2) {
							this.secondstate = true;
						}
						// 判断是否有单位变化规则
						if (this.detailInfo.uom_lines) {
							this.uomlines(this.detailInfo.uom_lines);
						}
						if (this.detailInfo.uom_lines) {
							this.uomlines(this.detailInfo.uom_lines);
						}
						if (this.detailInfo.uom_lines.length == 1) {
							this.detailInfo.position_area = Math.ceil((this.detailInfo.loss_coefficient / 100 + 1) * this.detailInfo.position_area);
							this.detailInfo.need_qty = Math.ceil(((this.detailInfo.loss_coefficient / 100 + 1) * this.detailInfo.position_area) / this.ruleVariable2);
							this.detailInfo.product_qty = this.detailInfo.need_qty;
							console.log(this.ruleVariable2);
						}
						if (this.detailInfo.uom_lines.length == 2) {
							this.detailInfo.position_area = Math.ceil((this.detailInfo.loss_coefficient / 100 + 1) * this.detailInfo.position_area);
							this.detailInfo.need_qty = Math.ceil(((this.detailInfo.loss_coefficient / 100 + 1) * this.detailInfo.position_area) / this.ruleVariable2);
							this.detailInfo.product_qty = Math.ceil(this.detailInfo.need_qty / this.ruleVariable1);
						}
						// 当position_area为0 时候

						if (this.detailInfo.position_area == 0) {
							this.detailInfo.position_area = '';
						}
						console.log(this.detailInfo.uom_lines.length);
					}
				},
				fail: () => {},
				complete: () => {}
			});
		},
		// 返回上级
		ReturnSuperiors() {
			this.$eventBus.$emit('measureComplete');
			uni.navigateBack({
				delta: 1
			});
		}
	}
};
</script>

<style scoped>
.play-order-content {
	padding: 20upx 40upx;
	background-color: #ffffff;
	width: 100%;
}

.play-order-content .play-order-view {
	font-size: 32upx;
	height: 80upx;
	line-height: 80upx;
}

.play-order-content view text:nth-of-type(1) {
	color: #8f8f94;
}

.play-order-content view text:nth-child(2) {
	padding-left: 20upx;
}

.required-number {
	display: flex;
	align-items: center;
}

.required-number input {
	width: 160upx;
	border-bottom: 2upx solid #8f8f8f;
	text-align: center;
}
</style>
